package com.yang.system.domain.orgMng.org.validator;

import com.yang.system.domain.common.exception.BusinessException;
import com.yang.system.domain.common.exception.DirtyDataException;
import com.yang.system.domain.orgMng.org.Org;
import com.yang.system.domain.orgMng.org.OrgRepository;
import com.yang.system.domain.orgMng.org.OrgStatus;
import com.yang.system.domain.orgMng.orgType.OrgTypeStatus;
import com.yang.system.domain.orgMng.orgType.OrgTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SuperiorValidator {

    private final OrgRepository orgRepository;
    private final OrgTypeRepository orgTypeRepository;

    @Autowired
    public SuperiorValidator(OrgRepository orgRepository, OrgTypeRepository orgTypeRepository) {
        this.orgRepository = orgRepository;
        this.orgTypeRepository = orgTypeRepository;
    }

    //校验上级组织的规则分组
    private void validateSuperior(Long tenantId, Long superiorId, String orgType) {
        Org superiorOrg = superiorShouldEffective(tenantId, superiorId);
        OrgTypeStatus superiorOrgTypeStatus = findSuperiorOrgType(tenantId, superiorId, superiorOrg);
        superiorOfDevGroupMustDevCenter(superiorId, orgType, superiorOrgTypeStatus);
        SuperiorOfDevCenterAndDirectDeptMustEntp(superiorId, orgType, superiorOrgTypeStatus);
    }

    // 上级组织应该是有效组织
    private Org superiorShouldEffective(Long tenantId,Long superiorId) {
        Org superior = orgRepository.findByIdAndStatus(tenantId
                        , superiorId, OrgStatus.EFFECTIVE)
                .orElseThrow(() -> new BusinessException(
                        "'" + superiorId + "' 不是有效的组织 id !")
                );
        return superior;
    }

    // 取上级组织的组织类别
    private OrgTypeStatus findSuperiorOrgType(Long tenantId, Long superiorId, Org superior) {
        OrgTypeStatus superiorOrgTypeStatus = (OrgTypeStatus) orgTypeRepository.findByCodeAndStatus(tenantId
                        , superior.getOrgTypeCode()
                        , OrgTypeStatus.EFFECTIVE)
                .orElseThrow(() ->
                        new DirtyDataException("id 为 '"
                                + superiorId
                                + "' 的组织的组织类型代码 '"
                                + superior.getOrgTypeCode() + "' 无效!"));
        return superiorOrgTypeStatus;
    }

    // 开发组的上级只能是开发中心
    private void superiorOfDevGroupMustDevCenter(Long superiorId,String orgType, OrgTypeStatus superiorOrgTypeStatus){
        if ("DEVGRP".equals(orgType) && !"DEVCENT".equals(superiorOrgTypeStatus.getCode())) {
            throw new BusinessException("开发组的上级(id = '"
                    + superiorId + "')不是开发中心！");
        }
    }


    // 开发中心和直属部门的上级只能是企业
    private void SuperiorOfDevCenterAndDirectDeptMustEntp(Long superiorId,String orgType, OrgTypeStatus superiorOrgTypeStatus) {
        if (("DEVCENT".equals(orgType) || "DIRDEP".equals(orgType))
                && !"ENTP".equals(superiorOrgTypeStatus.getCode())) {
            throw new BusinessException("开发中心或直属部门的上级(id = '"
                    + superiorId + "')不是企业！");
        }
    }

    public void verify(Long tenantId, Long superiorId, String orgTypeCode) {
        validateSuperior(tenantId, superiorId, orgTypeCode);
    }
}
